/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_SunSensor.c
 *
 *  Created on: 2023年2月12日
 *      Author: 64435
 */

#include "LOS_SunSensor.h"

LOS_SUNSSTA_t STA_SUNS;

uint8_t TMPACK_SUNSBasic[26];
//uint8_t TMPACK_SUNSImage[280];

void LOS_SunSensor_Set(uint8_t cmd1, uint8_t cmd2)
{
    LOS_SNP_t SNP_CMD;        //首部配置结构体
    uint8_t FP_CMD[8];        //首部内容
    
    SNP_CMD.SNP_dest = nID_SunSensor;   
    SNP_CMD.SNP_source = 0xF0;             //OBC发送
    SNP_CMD.SNP_rec = 0x0;                //内容识别码
    SNP_CMD.SNP_len = 11;                 //长度（包含首部）
    SNP_CMD.SNP_tran = 0x01;               //0001B,信息传递服务
    LOS_CAN_FirstPart(&SNP_CMD,FP_CMD);    //生成首部内容
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0X00), FP_CMD, 8);
    FP_CMD[0] = 0x8B;
    FP_CMD[1] = cmd1;
    FP_CMD[2] = cmd2; 
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0XFF), FP_CMD, 3);
}

void LOS_SunSensor_GetSta()
{
    LOS_SNP_t SNP_CMD;        //首部配置结构体
    uint8_t FP_CMD[8];        //首部内容
    STA_SUNS.HL_TXF = 1;
    
    SNP_CMD.SNP_dest = nID_SunSensor;   
    SNP_CMD.SNP_source = 0xF0;             //OBC发送
    SNP_CMD.SNP_rec = 0x00;                //内容识别码
    SNP_CMD.SNP_len = 10;                 //长度（包含首部）
    SNP_CMD.SNP_tran = 0x01;               //0001B,信息传递服务
    LOS_CAN_FirstPart(&SNP_CMD,FP_CMD);    //生成首部内容
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0X00), FP_CMD, 8);
    FP_CMD[0] = 0x90;
    FP_CMD[1] = 0x00;
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0XFF), FP_CMD, 2);
}

void LOS_SunSensor_GetImage()
{
    LOS_SNP_t SNP_CMD;        //首部配置结构体
    uint8_t FP_CMD[8];        //首部内容
    
    SNP_CMD.SNP_dest = nID_SunSensor;   
    SNP_CMD.SNP_source = 0xF0;             //OBC发送
    SNP_CMD.SNP_rec = 0x01;                //内容识别码
    SNP_CMD.SNP_len = 10;                 //长度（包含首部）
    SNP_CMD.SNP_tran = 0x01;               //0001B,信息传递服务
    LOS_CAN_FirstPart(&SNP_CMD,FP_CMD);    //生成首部内容
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0X00), FP_CMD, 8);
    FP_CMD[0] = 0x90;
    FP_CMD[1] = 0x11;
    LOS_CAN_Transmit(LOS_CAN_NUM1, LOS_CAN_IDGen(ACK_NoNeed, 0xF0, nID_SunSensor, 0XFF), FP_CMD, 2);
}

void LOS_CAN_SunSensorCallBack(LOS_CANMSG_t* can_re)
{
    /* 首部校验用法：首部校验通过/未通过会改变canerrflag的值，所有消息会被先存在缓冲区，canerrflag有效时才会转存，无效时舍弃 */
    uint8_t id_filter[4];
    uint8_t sum = 0;
    uint8_t sum_check = 0;
    static uint8_t canerrflag = 0;     //表示首部校验是否通过，未通过：1，通过：0
    static uint8_t canmsgrec = 0xFF;   //消息类型（对应首部的rec）
    float ang_buf = 0;                 //角度缓冲区
    uint8_t sta_buf;                   //状态缓冲区

    memcpy(id_filter, &(can_re->ID), 4);   //将帧ID分为4个字节存储，便于处理
    if (id_filter[3] == 0x00)        //若为首帧
    {
        sum = can_re->msg[0] + can_re->msg[1] + can_re->msg[2] + can_re->msg[3] + can_re->msg[4] + can_re->msg[5] + can_re->msg[6];
        sum_check = 0x100 - sum;
        if (sum_check == can_re->msg[7])   //校验和检查，消息有效性判断
        {
            canerrflag = 0;
            canmsgrec = can_re->msg[6];  
        }
        else 
        {
           canerrflag = 1; 
        }
    }
    else if (id_filter[3] == 0xFF)   //若为尾帧
    {
        if (canmsgrec == 0xF0)       //工参
        {
            memcpy(&ang_buf,&(can_re->msg[0]),4);
            STA_SUNS.alpha = ang_buf;
            memcpy(&ang_buf,&(can_re->msg[4]),4);
            STA_SUNS.beta = ang_buf;
            memcpy(&TMPACK_SUNSBasic[18], &can_re->msg[0], 8);
        }
        else if (canmsgrec == 0xF1)   //图像数据
        {
            //memcpy(&TMPACK_SUNSImage[274], &can_re->msg[0], 6);
        }
        STA_SUNS.MSG = 0;
        STA_SUNS.HL_TXF = 0;
        STA_SUNS.TMCNT++;
    }
    else                          //若为中间帧
    {
        if (canmsgrec == 0xF0)   //工参
        {
            if (id_filter[3] == 0x01)
            {
                STA_SUNS.sta = can_re -> msg[0];
                if (STA_SUNS.sta & 0x80)
                {
                    STA_SUNS.Flag_D = 1;
                }
                else
                {
                    STA_SUNS.Flag_D = 0;
                }
                
                if (STA_SUNS.sta & 0x40)
                {
                    STA_SUNS.OverSight = 1;
                }
                else
                {
                    STA_SUNS.OverSight = 0;
                }
                STA_SUNS.Attitude_Delay = can_re -> msg[1];
                STA_SUNS.Normalized_Threshold = can_re -> msg[2];
                STA_SUNS.Exposure_Time = can_re -> msg[3];

                memcpy(&TMPACK_SUNSBasic[0], &LOS_Time_SecFromBoot, 4);
                memcpy(&TMPACK_SUNSBasic[4], &LOS_Time_MilSec, 2);
                memcpy(&TMPACK_SUNSBasic[6], &LOS_Time_Sec, 4);
                memcpy(&TMPACK_SUNSBasic[10], &can_re->msg[0], 8);
                
            }
        }
        else if (canmsgrec == 0xF1)  //图像数据
        {
            if (id_filter[3] == 0x01)
            {

            }
            else
            {
                
            }
        }
    }
}

void LOS_SUNS_PWROn()
{
    LOS_EPS_Switch(12, SWITCH_ON);
    STA_SUNS.CMD = 1;
}

void LOS_SUNS_PWROff()
{
    LOS_EPS_Switch(12, SWITCH_OFF);
    STA_SUNS.MSG = 1;
    STA_SUNS.CMD = 0;
    STA_SUNS.REC = 0;
}


